﻿using System;
using System.Collections.Generic;
using System.Linq;
using Dost.Khcn.Data.EntityModel;
using Dost.Khcn.Data.Implements;
using Dost.Khcn.Data.Interfaces;

namespace Dost.Khcn.Biz
{
    public class LinhVucBiz
    {
        #region Properties

        private ILinhVucDao _linhVucDao;
        private ILinhVucDao LinhVucData
        {
            get { return _linhVucDao ?? (_linhVucDao = new LinhVucDao()); }
        }

        #endregion

        #region Constructors

        #endregion

        #region LinhVuc Methods

        public List<LinhVuc> GetAllLinhVucs(int nhomLinhVucId = 0)
        {
            return LinhVucData.GetAll(nhomLinhVucId);
        }

        public object GetLinhVucs(int startIndex, int pageSize, string sorting, string name, string code, int nhomLinhVucId)
        {
            try
            {
                int totalRecords;
                var linhVucs = LinhVucData.GetLinhVucs(startIndex, pageSize, sorting, name, nhomLinhVucId, code, out totalRecords);
                var records = linhVucs.Select(x => new { x.Id, x.Ma, x.Ten, x.Mota, x.NhomLinhVucId });
                return new { Result = "OK", Records = records, TotalRecordCount = totalRecords };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public LinhVuc CreateLinhVuc(LinhVuc linhVuc)
        {
            // validate inputs
            if (linhVuc == null) return null;
            if (linhVuc.NhomLinhVucId == 0)
            {
                linhVuc.NhomLinhVucId = null;
            }

            return string.IsNullOrEmpty(linhVuc.Ten) ? null : LinhVucData.AddLinhVuc(linhVuc);
        }

        public LinhVuc GetById(int id)
        {
            return id > 0 ? LinhVucData.GetById(id) : null;
        }

        public void UpdateLinhVuc(LinhVuc linhVuc)
        {
            if (linhVuc == null) return;
            if (linhVuc.NhomLinhVucId == 0)
            {
                linhVuc.NhomLinhVucId = null;
            }
            LinhVucData.UpdateLinhVuc(linhVuc);
        }

        public object DeleteLinhVuc(int id)
        {
            try
            {
                LinhVucData.DeleteLinhVuc(id);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        #endregion

        #region NhomLinhVuc Methods

        public List<NhomLinhVuc> GetAllNhomLinhVucs()
        {
            return LinhVucData.GetAllNhomLinhVucs();
        }

        public object GetNhomLinhVucs(int startIndex, int pageSize, string sorting, string name, string code)
        {
            try
            {
                int totalRecords;
                var nhomLinhVucs = LinhVucData.GetNhomLinhVucs(startIndex, pageSize, sorting, name, code, out totalRecords);
                var records = nhomLinhVucs.Select(x => new { x.Id, x.Ma, x.Ten, x.Mota });
                return new { Result = "OK", Records = records, TotalRecordCount = totalRecords };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public NhomLinhVuc CreateNhomLinhVuc(NhomLinhVuc nhomLinhVuc)
        {
            // validate inputs
            if (nhomLinhVuc == null) return null;
            if (string.IsNullOrEmpty(nhomLinhVuc.Ma) || string.IsNullOrEmpty(nhomLinhVuc.Ten)) return null;

            return LinhVucData.AddNhomLinhVuc(nhomLinhVuc);
        }

        public void UpdateNhomLinhVuc(NhomLinhVuc nhomLinhVuc)
        {
            if (nhomLinhVuc == null) return;
            if (string.IsNullOrEmpty(nhomLinhVuc.Ma) || string.IsNullOrEmpty(nhomLinhVuc.Ten)) return;

            LinhVucData.UpdateNhomLinhVuc(nhomLinhVuc);
        }

        public object DeleteNhomLinhVuc(int id)
        {
            try
            {
                LinhVucData.DeleteNhomLinhVuc(id);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        #endregion
    }
}
